package com.szqhyxj.youxiangju.biz.receipt.receiptdetail.signature;

import com.szqhyxj.youxiangju.biz.core.ImageUploadResult;
import com.szqhyxj.youxiangju.biz.core.ImageUploader;
import com.szqhyxj.youxiangju.biz.creditReport.CreditReportEntity;
import com.szqhyxj.youxiangju.biz.creditReport.CreditReportModel;
import com.szqhyxj.youxiangju.sys.BaseResult;
import com.szqhyxj.youxiangju.sys.util.BitmapCompressFile;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by leung on 2018/11/4.
 */

public class SignaturePresenter implements SignatureContract.Presenter {
    SignatureContract.View view;
    private List<Disposable> disposables = new ArrayList<>();
    CreditReportEntity creditReportEntitys;

    public SignaturePresenter(SignatureContract.View view) {
        this.view = view;
    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void postSign(String cardno, String amount, File file) {
        view.showLoading();
        File tfile = BitmapCompressFile.compressFile(file);

        Disposable disposable = ImageUploader
                .uploadqucikSign(tfile)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        new Consumer<BaseResult<ImageUploadResult>>() {
                            @Override
                            public void accept(BaseResult<ImageUploadResult> uploadResult) {
                                view.dismissLoading();
                                if (uploadResult.businessLogicSuccess()) {
                                    String url = uploadResult.getData().getFileUrl();
                                    signCommit(cardno, amount, url);
                                } else {
                                    view.showFile(uploadResult.getMessage());

                                }
                                tfile.delete();
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) {
                                view.dismissLoading();
                                view.showFile(throwable.getMessage());
                                tfile.delete();
                            }
                        }
                );
        disposables.add(disposable);
    }

    private static final int MINUTE = 60;

    @Override
    public void getMsg(String cardphone) {
        view.showLoading();
        SigntureModel.getmsg(cardphone)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult>() {
                    @Override
                    public void accept(BaseResult baseResult) throws Exception {
                        view.dismissLoading();
                        if (baseResult.businessLogicSuccess()) {
                            view.showAcquiredVerificationCodeSuccess();
                            view.showAcquiringVerificationCode(MINUTE);
                            startCountDown();
                        } else {
                            view.showAcquiredVerificationCodeFail(baseResult.getMessage());
                            view.showAcquiredVerificationCode();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.dismissLoading();
                        view.showToast(throwable.getMessage());
                    }
                });
    }

    @Override
    public void verificationMsg(String cardPhone, String msg) {
        view.showLoading();
        SigntureModel.verificationmsg(msg, cardPhone)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult>() {
                    @Override
                    public void accept(BaseResult baseResult) throws Exception {
                        view.dismissLoading();
                        if (baseResult.businessLogicSuccess()) {
                            view.showVerificationSuccess();
                        } else {
                            view.showToast(baseResult.getMessage() + "");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.dismissLoading();
                        view.showToast(throwable.getMessage());
                    }
                });
    }

    /**
     * 获取验证码
     */
    private void startCountDown() {
        disposables.add(Observable
                .interval(0, 1, TimeUnit.SECONDS)
                .take(MINUTE)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) {
                        if (aLong == MINUTE - 1) {
                            view.showAcquiredVerificationCode();
                        } else {
                            view.showAcquiringVerificationCode((int) (MINUTE - aLong - 1));
                        }
                    }
                }));
    }

    public void signCommit(String cardno, String amount, String url) {
        view.showLoading();
        SigntureModel.getCommitmerchant(cardno, new BigDecimal(amount), url)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult>() {
                    @Override
                    public void accept(BaseResult baseResult) throws Exception {
                        view.dismissLoading();
                        if (baseResult.businessLogicSuccess()) {
                            view.reportSuccess();
                        } else {
                            view.showFile(baseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.dismissLoading();
                        view.showFile(throwable.getMessage());
                    }
                });
    }


}
